<template>
  <div class="dialog-box" style="position: relative;">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="formEditPmDictDataRef"
        :model="formData"
        :size="defaultFormItemSize"
        :rules="rules"
        label-width="120px"
        label-position="right"
        @submit.native.prevent
      >
        <el-row :gutter="16">
          <el-col :span="24">
            <el-form-item label="字典键" prop="PmDictData.dictValue">
              <el-input
                class="input-item"
                v-model="formData.PmDictData.dictValue"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典值" prop="PmDictData.dictLabel">
              <el-input
                class="input-item"
                v-model="formData.PmDictData.dictLabel"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典排序" prop="PmDictData.dictSort">
              <el-input-number
                class="input-item"
                v-model="formData.PmDictData.dictSort"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="父节点" prop="PmDictData.parentId">
              <el-select
                class="input-item"
                v-model="formData.PmDictData.parentId"
                placeholder=""
                :clearable="true"
                :filterable="true"
                @visible-change="formEditPmDictData.parentIdWidget.onVisibleChange"
              >
                <el-option
                  v-for="item in formEditPmDictData.parentIdWidget.dropdownList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="PmDictData.remark">
              <el-input
                class="input-item"
                v-model="formData.PmDictData.remark"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <el-row class="footer-box" type="flex" justify="end" align="middle">
      <el-button :size="defaultFormItemSize" @click="onCancel()">取消</el-button>
      <el-button :size="defaultFormItemSize" type="primary" @click="onSubmitPmDictDataClick()">保存</el-button>
    </el-row>
  </div>
</template>

<script>
import rules from '@/utils/validate';
import { stringCase, treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode } from '@/utils';
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
import { PmDictDataController, DictionaryController } from '@/api';

export default {
  name: 'formEditPmDictData',
  props: {
    dataId: {
      default: undefined
    },
    dictId: {
      default: undefined
    },
    subPage: {
      type: [Number, String, Boolean],
      default: 0
    },
    saveOnSubmit: {
      type: Boolean,
      default: true
    },
    rowData: {
      type: Object
    }
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      formData: {
        PmDictData: {
          // 主键ID
          dataId: undefined,
          // 字典编码
          dictId: undefined,
          // 字典标签
          dictLabel: undefined,
          // 字典值
          dictValue: undefined,
          // 字典排序
          dictSort: undefined,
          // 父级ID(树形字典使用)
          parentId: undefined,
          // 是否默认(Y-是,N-否)
          isDefault: undefined,
          // 状态(0-正常,1-停用)
          status: undefined,
          // 备注
          remark: undefined,
          isDatasourceInit: false
        }
      },
      formEditPmDictData: {
        // 父级ID(树形字典使用)组件
        parentIdWidget: new DropdownWidget(this.loadParentIdDropdownList),
        isInit: false
      },
      rules: {
        'PmDictData.dictValue': [
          {required: true, message: '请输入字典键', trigger: 'blur'}
        ],
        'PmDictData.dictSort': [
        ],
        'PmDictData.parentId': [
        ],
        'PmDictData.remark': [
        ],
        'PmDictData.dictLabel': [
          {required: true, message: '请输入字典值', trigger: 'blur'}
        ]
      },
    };
  },
  methods: {
    onCancel(isSuccess = false, data = null) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess, data);
      }
    },
    /**
     * 重置过滤值
     */
    resetFormEditPmDictData() {
      this.refreshFormEditPmDictData();
    },
    /**
     * 重置所有过滤值
     */
    resetFilter () {
      this.resetFormEditPmDictData();
    },
    /**
     * 保存
     */
    onSubmitPmDictDataClick() {
      this.$refs.formEditPmDictDataRef.validate((valid) => {
        if (!valid) return;
        // 级联操作
        if (!this.saveOnSubmit) {
          let formData = {
            ...this.formData.PmDictData
          };
        formData.parentIdDictMap = findItemFromList(this.formEditPmDictData.parentIdWidget.dropdownList, formData.parentId, 'id');
          this.onCancel(true, formData);
          return;
        }
        let params = {
          pmDictDataDto: {
            dataId: this.formData.PmDictData.dataId,
            dictId: this.formData.PmDictData.dictId,
            dictLabel: this.formData.PmDictData.dictLabel,
            dictValue: this.formData.PmDictData.dictValue,
            dictSort: this.formData.PmDictData.dictSort,
            parentId: this.formData.PmDictData.parentId,
            isDefault: this.formData.PmDictData.isDefault,
            status: this.formData.PmDictData.status,
            remark: this.formData.PmDictData.remark,
          }
        };

        let httpCall = this.isEdit ? PmDictDataController.update : PmDictDataController.add;
        httpCall(this, params).then(res => {
          this.$message.success('保存成功');
          this.onCancel(true);
        }).catch(e => {});
      });
    },
    loadPmDictDataData() {
      return new Promise((resolve, reject) => {
        if (!this.isEdit) {
          this.formData.PmDictData = {dictId: this.dictId};
          resolve();
          return;
        }
        if (!this.saveOnSubmit && this.rowData != null) {
          this.formData.PmDictData = JSON.parse(JSON.stringify(this.rowData));
          resolve();
          return;
        }
        let params = {
          dataId: this.dataId
        };
        PmDictDataController.view(this, params).then(res => {
          this.formData.PmDictData = {...res.data, isDatasourceInit: true};
          resolve();
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 父级ID(树形字典使用)下拉数据获取函数
     */
    loadParentIdDropdownList() {
      return new Promise((resolve, reject) => {
        const params = {
        };
        DictionaryController.dictPmDictData(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    onUploadError (e, file, fileList) {
      this.$message.error('文件上传失败');
    },
    onUploadLimit (files, fileList) {
      this.$message.error('已经超出最大上传个数限制');
    },
    refreshFormEditPmDictData(reloadData = false) {
    },
    formInit() {
      console.log(this.dictId)
      console.log(this.dictId !=null)
      console.log(this.isEdit)
      this.loadPmDictDataData().then(res => {
        this.formEditPmDictData.parentIdWidget.onVisibleChange(true).catch(e => {});
        if (this.isEdit) this.refreshFormEditPmDictData();
      }).catch(e => {});
    }
  },
  computed: {
    isEdit() {
      return this.saveOnSubmit ? this.dataId != null : this.rowData != null;
    }
  },
  mounted() {
    // 初始化页面数据
    this.formInit();
  }
};
</script>
